<template>
  <div>
    <div class="table-page-search-wrapper">
      <a-form-model
        layout="inline"
        :model="queryParam"
      >
        <a-row :gutter="15">
          <a-col :md="6" :sm="24">
            <a-form-model-item label="业务名称">
              <a-input v-model="queryParam['filter[business_name]']" placeholder="请输入业务名称" />
            </a-form-model-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-model-item label="合同编号">
              <a-input v-model="queryParam['filter[contract_no]']" placeholder="请输入合同编号" />
            </a-form-model-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务编号">
                <a-input v-model="queryParam['filter[business_no]']" placeholder="请输入业务编号" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="专利名称">
                <a-input v-model="queryParam['filter[zl_name]']" placeholder="请输入专利名称" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="提交时间">
                <a-range-picker @change="onChangeSubmit" />
              </a-form-item>
            </a-col>
            <!--              <a-col :md="6" :sm="24">-->
            <!--                <a-form-item label="领取时间">-->
            <!--                  <a-range-picker @change="onChangeReceive" />-->
            <!--                </a-form-item>-->
            <!--              </a-col>-->
            <a-col :md="6" :sm="24">
              <a-form-model-item label="所属顾问">
                <a-select
                  show-search
                  v-model="queryParam['filter[adviser_uid]']"
                  placeholder="输入要搜索的顾问姓名"
                  :default-active-first-option="false"
                  :show-arrow="false"
                  :filter-option="false"
                  :not-found-content="null"
                  @search="onSearch"
                >
                  <a-select-option v-for="(item,index) in searchSource" :key="index" :value="index">
                    {{ item }}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24" v-if="groupType === 'proced_zl_engineer_leader'">
              <a-form-model-item label="专利流程">
                <a-select v-model="queryParam['filter[receive_uid]']" placeholder="请选择专利流程">
                  <a-select-option v-for="(item,index) in processOpts" :key="index" :value="index">{{item}}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col v-if="group == 'small_leader'" :md="6" :sm="24">
              <a-form-model-item label="专利老师">
                <a-select v-model="queryParam['filter[patenteer]']" placeholder="请选择专利老师">
                  <a-select-option v-for="(item,index) in patentOpts" :key="index" :value="index">{{item}}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
          </template>
          <a-col :md="!advanced && 8 || 24" :sm="24">
                <span class='table-page-search-submitButtons' :style="advanced && { float: 'right', overflow: 'hidden' } || {}">
                  <a-button type="primary" @click="$refs.table.refresh(true)">搜索</a-button>
                  <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
                  <a @click="toggleAdvanced" style="margin-left: 8px">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'" />
                  </a>
                </span>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <div class="table-operator">
      <s-table
        bordered
        size="middle"
        ref="table"
        :row-key="(record) => record.cb_id"
        :columns="feedColumns"
        :data="feedData"
      >
        <template slot="contract_no" slot-scope="text,record">
          {{text}}<br>
          <a @click="handleButtonClick(record.cb_id)">{{record.business_no}}</a>
        </template>
        <template slot="status2" slot-scope="text,record">
          <a-button v-if="record.status2_code == 22 || record.status2_code == 20 || record.status2_code == 35" size="small" icon="edit" type="danger" @click="changeStatus(record.cb_id)">{{text}}</a-button>
          <a-button
            ghost
            v-if="record.status2_code == 40 || record.status2_code == 30"
            type="danger"
            size="small"
            @click="changeStatus(record.cb_id)"
          >
            {{text}}
          </a-button>
          <a-button
            ghost
            v-if="record.status2_code == 24 || record.status2_code == 32 || record.status2_code == 34 || record.status2_code == 36"
            type="primary"
            size="small"
            @click="changeStatus(record.cb_id)"
          >
            {{text}}
          </a-button>
        </template>
        <template slot="patenteer_filename" slot-scope="text,record">
          <a v-if="text != ''" @click="download(record.patenteer_file)"><a-icon type="download" /> {{text}}</a>
          <a-tag v-else color="red">暂无材料</a-tag>
        </template>
        <template slot="submit_update_time" slot-scope="text,record">
          <a-statistic-countdown :value="text * 1000 + 1000 * 60 * 60 * 24 * record.revision_time" format="D天H时m分s秒" />
        </template>
        <template slot="action" slot-scope="text,record">
          <a-button size="small" type="danger" @click="getUpload(record.cb_id)">上传稿件</a-button>
        </template>
      </s-table>
    </div>
    <a-modal
      centered
      title="上传专利师撰写材料"
      :confirm-loading="loading"
      v-model="uploadVisible"
      @ok="submitUpload"
    >
      <a-form-model
        :model="uploadForm"
      >
        <a-form-model-item label="业务名称">
          <a-input disabled v-model="uploadForm.business_name" type="text" />
        </a-form-model-item>
        <a-form-model-item label="上传专利师撰写材料">
          <a-upload
            name="file"
            accept=".rar,.zip,.7z"
            :multiple="true"
            :headers="headers"
            :action="uploadUrl"
            :show-upload-list="false"
            @change="onChange"
          >
            <a-button type="danger"> <a-icon type="upload" />上传整理后的材料</a-button>
          </a-upload>
        </a-form-model-item>
        <a-button v-if="uploadForm.patenteer" size="small" type="link" icon="download" @click="() => window.open(uploadForm.patenteer.file)"><ellipsis :length="30">{{uploadForm.patenteer.filename}}</ellipsis></a-button>
      </a-form-model>
    </a-modal>
    <a-modal
      centered
      v-model="submitVisible"
      :confirm-loading="loading"
      title="编辑状态"
      @ok="submitChangeStatus"
    >
      <a-form-model
        :model="changeStatusForm"
      >
        <a-form-model-item>
          <a-radio-group v-model="changeStatusForm.status2">
            <a-radio v-for="(item,index) in changeStatusOpts" :key="index" :value="index">
              {{item}}
            </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item>
          <a-textarea v-model="changeStatusForm.notes" :rows="4" placeholder="请输入材料备注"></a-textarea>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import moment from 'moment'
import { Ellipsis, STable } from '@/components'
import businessColumns from '@/columns/patent.process'
import {
  getCaseList,
  getCaseStatus,
  getCaseUpload,
  onCompletionAdviser,
  postCaseStatus,
  postCaseUpload
} from '@/api/patent.process'
import storage from 'store'
import { ACCESS_TOKEN, GROUP, ROLE } from '@/store/mutation-types'
import apis from '@/api'

export default {
  components: {
    STable,
    Ellipsis
  },
  name: 'FeedBack',
  data () {
    return {
      moment,
      feedColumns: businessColumns.feedback,
      feedData: parameters => {
        return getCaseList(Object.assign(parameters, this.queryParam)).then(res => {
          this.patentOpts = res.result.patenteer_opts
          this.processOpts = res.result.flow
          return res.result
        })
      },
      group: storage.get(GROUP),
      groupType: storage.get(ROLE),
      queryParam: {},
      searchSource: [],
      advanced: true,
      loading: false,
      processOpts: {},
      patentOpts: {},
      submitVisible: false,
      changeStatusForm: {},
      changeStatusOpts: {},
      uploadVisible: false,
      uploadUrl: apis.Upload,
      uploadForm: {},
      headers: {
        'Access-Token': storage.get(ACCESS_TOKEN)
      }
    }
  },
  created () {
    this.queryParam['filter[status2]'] = '35'
  },
  methods: {
    getUpload (id) {
      getCaseUpload(id).then(res => {
        this.uploadForm = res.result
        this.uploadForm['cb_id'] = id
        this.uploadVisible = true
      })
    },
    submitUpload () {
      const app = this
      const data = {}
      data['patenteer[file]'] = app.uploadForm.patenteer.file
      data['patenteer[filename]'] = app.uploadForm.patenteer.filename
      postCaseUpload(this.uploadForm['cb_id'], data).then(res => {
        if (res.status === 0) {
          app.uploadVisible = false
          app.$refs.table.refresh()
        } else {
          app.$warning({
            title: res.msg
          })
        }
      })
    },
    onChange (info) {
      if (info.file.status === 'uploading') {
        this.loading = true
        return false
      }
      if (info.file.status === 'done') {
        this.uploadForm.patenteer.filename = info.file.name
        this.uploadForm.patenteer.file = info.file.response.result.url
        this.loading = false
      }
    },
    submitChangeStatus () {
      const app = this
      const data = {}
      app.loading = true
      data['contract_business[status2]'] = app.changeStatusForm.status2
      data['contract_business[notes]'] = app.changeStatusForm.notes
      postCaseStatus(app.changeStatusForm['cb_id'], data).then(res => {
        if (res.status === 0) {
          app.onCancel()
          app.$refs.table.refresh()
        } else {
          app.$warning({
            title: res.msg
          })
        }
      }).finally(() => {
        app.loading = false
      })
    },
    changeStatus (id) {
      const app = this
      app.changeStatusForm = {}
      app.changeStatusForm['cb_id'] = id
      getCaseStatus(id).then(res => {
        if (res.status === 0) {
          app.changeStatusOpts = res.result.status2_items
          app.submitVisible = true
        } else {
          app.$warning({
            title: res.msg
          })
        }
      })
    },
    onChangeSubmit (date, dateString) {
      this.queryParam['filter[submit_mats_time_start]'] = dateString[0]
      this.queryParam['filter[submit_mats_time_end]'] = dateString[1]
    },
    onChangeReceive (date, dateString) {
      this.queryParam['filter[received_at_start]'] = dateString[0]
      this.queryParam['filter[received_at_end]'] = dateString[1]
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    download (url) {
      window.open(url)
    },
    resetForm () {
      this.queryParam = {}
      this.$refs.table.refresh(true)
    },
    onSearch (searchText) {
      onCompletionAdviser(searchText).then(res => {
        this.searchSource = res.result.data
      })
    },
    handleButtonClick (id) {
      // this.$router.push({ name: 'TaskEdit', params: { id } })
      if (this.groupType === 'proced_zl_engineer') {
        this.$router.push({ name: 'BusinessEdit', params: { id } })
      } else {
        this.$router.push({ name: 'AdminDetail', params: { id } })
      }
    },
    onCancel () {
      this.changeStatusForm = {}
      this.submitVisible = false
    }
  }
}
</script>

<style scoped>
/deep/ .ant-statistic-content {
  font-size: 16px;
  color: #f00;
}
/deep/ .ant-table-tbody > tr.table-color-dust {
  background-color: #ffcccc;
}
</style>
